package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.guesstv.lib.module.GtDataProvider;
import com.stark.guesstv.lib.module.bean.TvActorBean;
import com.stark.idiom.lib.model.bean.Idiom;
import com.stark.idiom.lib.model.db.IdiomDbHelper;
import com.stark.riddle.lib.model.bean.Riddle;
import com.stark.riddle.lib.model.bean.Saying;
import com.stark.riddle.lib.model.db.RiddleDaoHelperManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelLevelAdapter;
import flc.ast.databinding.ActivitySelLevelBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.inf.IRetCallback;

public class SelLevelActivity extends BaseAc<ActivitySelLevelBinding> {

    public static int type = 0;//0谜语
    public static String kindName = "";
    private List<Integer> listShow = new ArrayList<>();//展示的数据
    private SelLevelAdapter selLevelAdapter;


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_level;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSelLevelBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvLevelList.setLayoutManager(new GridLayoutManager(mContext, 5));
        selLevelAdapter = new SelLevelAdapter();
        mDataBinding.rvLevelList.setAdapter(selLevelAdapter);
        selLevelAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 0) {
            RiddleActivity.level = position + 1;
            startActivity(RiddleActivity.class);
        } else if (type == 1) {
            SayingActivity.level = position + 1;
            startActivity(SayingActivity.class);
        } else if (type == 2) {
            IdiomActivity.level = position + 1;
            startActivity(IdiomActivity.class);
        } else if (type == 3) {
            TeleplayActivity.level = position + 1;
            startActivity(TeleplayActivity.class);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (type == 0) {
            getRiddleLevel();
            mDataBinding.tvSelLevelTitle.setText(getString(R.string.riddle_title));
        } else if (type == 1) {
            getSayingLevel();
            mDataBinding.tvSelLevelTitle.setText(getString(R.string.saying_title));
        } else if (type == 2) {
            getIdiomLevel();
            mDataBinding.tvSelLevelTitle.setText(getString(R.string.idiom_title));
        } else if (type == 3) {
            getTvLevel();
            mDataBinding.tvSelLevelTitle.setText(getString(R.string.teleplay_title));
        }
    }

    //获取谜语关卡
    private void getRiddleLevel() {
        RiddleDaoHelperManager.getRiddleDbHelper().getByKind(kindName, 0, 80, new IRetCallback<List<Riddle>>() {
            @Override
            public void onResult(List<Riddle> result) {
                if (result != null && result.size() > 0) {
                    setData();
                }
            }
        });
    }

    //获取歇后语关卡
    private void getSayingLevel() {
        RiddleDaoHelperManager.getSayingDbHelper().getByKind(kindName, 0, 80, new IRetCallback<List<Saying>>() {
            @Override
            public void onResult(List<Saying> result) {
                if (result != null && result.size() > 0) {
                    setData();
                }
            }
        });
    }

    //获取猜成语关卡
    private void getIdiomLevel() {
        List<Integer> list = new ArrayList<>();
        IdiomDbHelper.get(list, 4, 80, 100, new IRetCallback<List<Idiom>>() {
            @Override
            public void onResult(List<Idiom> result) {
                if (result != null && result.size() > 0) {
                    //分页设置关卡数据
                    setData();
                }
            }
        });
    }

    //获取猜电视剧关卡
    private void getTvLevel() {
        List<TvActorBean> tvData = GtDataProvider.getTvData();
        List<TvActorBean> list = new ArrayList<>();
        for (int i = 0; i < tvData.size(); i++) {
            if (tvData.get(i).name.length() == 4) {//过滤出四字答案关卡
                list.add(tvData.get(i));
            }
            if (list.size() >= 80) {
                break;
            }
        }
        setData();
    }

    //设置数据
    private void setData() {
        listShow.clear();
        for (int i = 0; i < 80; i++) {
            listShow.add(i);
        }
        selLevelAdapter.setList(listShow);
        //根据关卡名称获取过关数量
        selLevelAdapter.setType(type);
        selLevelAdapter.setKindName(kindName);
        selLevelAdapter.notifyDataSetChanged();
    }

}